-- Author: shunguo.chen
-- Date: 2016-06-17 11:42:45
--
-- ShareController
--

local ShareView = import("..views.ShareView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(params)
	self.super.ctor(self, params)
	
end

function ClassRef:init()
	self._model = app:getInst("ShareModel")

	-- self._achieveHandler = UD:onChanged("achievement",handler(self._model, self._model.updateAchievement))
	self._mountHandler = UD:onChanged("mount",handler(self._model, self._model.updateMount))
end

function ClassRef:removeListeners()
	self.super.removeListeners(self)
	-- UD:unChanged("achievement", self._achieveHandler)
	UD:unChanged("mount", self._mountHandler)
end

function ClassRef:listEvent()
    return {
	    "onBattleSceenCapture"
	}
end


function ClassRef:onEvent(eventName, data)
	if eventName == "onBattleSceenCapture" then
		if data then
			self:closeView()
		end
	end
end

-- data = {type=, param=
-- type == 1 截屏sprite}
-- type == 2 坐骑tid, mounts=, gotNum=, maxNum=}
-- type == 3 击杀boss难度	, bossDatas=, compData=}
function ClassRef:openView(data)
	if cc.isDead(self._view) then
		if data.type == 2 then
			data.mounts = self._model:getMountData()
			data.gotNum, data.maxNum = self._model:getMountCompData()
		elseif data.type == 3 then
			data.num = self._model:getDiffNum()
			data.bossDatas = self._model:getBossDatas()
			data.compData = self._model:getBossCompData()
		end
		self._view = ShareView.new(nil, data)
		self._view:onCloseView(handler(self, self.closeView))
		self._view:onClearView(handler(self, self.clearView))
		self._view:onOperate(handler(self, self.onOperate))
		self:addChild(self._view, data)
	end
	self._view:show()
end

function ClassRef:shareBoss(diff)
	self:openView({type=3, param=diff or 1})
end

function ClassRef:shareMount(tid)
	self:openView({type=2, param=tid})
end

function ClassRef:shareScreenshot(cb)
	custom.captureScreen(function(isSuc, texture)
        if not isSuc then return end
        texture:setAntiAliasTexParameters()
        local sprite = display.newSprite(texture)
        sprite:setBlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
        sprite:setPosition(display.cx, display.cy)

        if cb then
                
        else
        	self:openView({type=1, param=sprite})
        end
    end)
end

function ClassRef:saveScreenshot(name, cb)
	display.captureScreen(function(isSuc, filePath)
        if not isSuc then return end
        print("saveScreenshot filePath", filePath)
        executeFunc(cb, filePath)
    end, name)
end

function ClassRef:saveShareView(name, cb)
	local renderTexture = cc.RenderTexture:create(display.width, display.height)
  	renderTexture:begin()
  	self._view:shareVisit()
  	renderTexture:endToLua()
  	renderTexture:saveToFile(name, 0, false, function(texture, path)
  		print("saveNodeShot.path", path)
  		executeFunc(cb, path)
  	end)
end

function ClassRef:saveNodeShot(node)
	local pos = node:convertToWorldSpace(cc.p(0,0))
	local rect = node:getContentSize()
	local bdRect = node:getBoundingBox()
	local scale = node:getScale()
	dump(pos, "pos")
	dump(rect, "rect")
	dump(bdRect, "bdRect")
	print("scale", scale)

	-- bdRect.width = bdRect.width * 0.7
	-- bdRect.height = bdRect.height * 0.7

	local direct = cc.Director:getInstance()
	local origin = direct:getVisibleOrigin()
	local full_rect = cc.rect(origin.x, origin.y, bdRect.width, bdRect.height)

	local renderTexture = cc.RenderTexture:create(bdRect.width, bdRect.height)
	renderTexture:setVirtualViewport(pos, full_rect, bdRect)
  	renderTexture:begin()
  	direct:getRunningScene():visit()
  	renderTexture:endToLua()
  	renderTexture:saveToFile("node_shot.png", 1, true, function(texture, path)
  		print("saveNodeShot.path", path)

  	end)
end

------------------------------ view logic ---------------------------------
function ClassRef:onOperate(op, data)
	if op == "shareQQ" then
		self:saveShareView("shareScreen.jpg", function(path)
			print("shareScreen", path)
			self._model:share(Enums.ShareType.qqZone, L("share_1001"), path, data.type)
		end)
	elseif op == "shareWX" then
		self:saveShareView("shareScreen.jpg", function(path)
			print("shareScreen", path)
			self._model:share(Enums.ShareType.wxCircle, L("share_1001"), path, data.type)
		end)
	end
end

return ClassRef